package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.FeedTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.PigTypeDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;

import cn.edu.scau.cmi.hombio.domain.FeedTaskQuality;
import cn.edu.scau.cmi.hombio.domain.PigType;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;

import cn.edu.scau.cmi.hombio.service.TaskPigstyService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for TaskPigsty entities
 * 
 */

@Controller("TaskPigstyRestController")

public class TaskPigstyRestController {

	/**
	 * DAO injected by Spring that manages FeedTaskQuality entities
	 * 
	 */
	@Autowired
	private FeedTaskQualityDAO feedTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages PigType entities
	 * 
	 */
	@Autowired
	private PigTypeDAO pigTypeDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyService taskPigstyService;

	/**
	 * Save an existing PigType entity
	 * 
	 */
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigType", method = RequestMethod.PUT)
	@ResponseBody
	public PigType saveTaskPigstyPigType(@PathVariable Integer taskpigsty_id, @RequestBody PigType pigtype) {
		taskPigstyService.saveTaskPigstyPigType(taskpigsty_id, pigtype);
		return pigTypeDAO.findPigTypeByPrimaryKey(pigtype.getId());
	}

	/**
	* Show all FeedTaskQuality entities by TaskPigsty
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/feedTaskQualities", method = RequestMethod.GET)
	@ResponseBody
	public List<FeedTaskQuality> getTaskPigstyFeedTaskQualities(@PathVariable Integer taskpigsty_id) {
		return new java.util.ArrayList<FeedTaskQuality>(taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id).getFeedTaskQualities());
	}

	/**
	* Get Pigsty entity by TaskPigsty
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigsty", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty getTaskPigstyPigsty(@PathVariable Integer taskpigsty_id) {
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id).getPigsty();
	}

	/**
	* Delete an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigsty/{pigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigstyPigsty(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_pigsty_id) {
		taskPigstyService.deleteTaskPigstyPigsty(taskpigsty_id, related_pigsty_id);
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTaskPigstyTask(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Save an existing FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/feedTaskQualities", method = RequestMethod.PUT)
	@ResponseBody
	public FeedTaskQuality saveTaskPigstyFeedTaskQualities(@PathVariable Integer taskpigsty_id, @RequestBody FeedTaskQuality feedtaskqualities) {
		taskPigstyService.saveTaskPigstyFeedTaskQualities(taskpigsty_id, feedtaskqualities);
		return feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskqualities.getId());
	}

	/**
	* View an existing PigType entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigType/{pigtype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigType loadTaskPigstyPigType(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_pigtype_id) {
		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype_id, -1, -1);

		return pigtype;
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTaskPigstyTask(@PathVariable Integer taskpigsty_id, @RequestBody Task task) {
		taskPigstyService.saveTaskPigstyTask(taskpigsty_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Get PigType entity by TaskPigsty
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigType", method = RequestMethod.GET)
	@ResponseBody
	public PigType getTaskPigstyPigType(@PathVariable Integer taskpigsty_id) {
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id).getPigType();
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newTaskPigstyTask(@PathVariable Integer taskpigsty_id, @RequestBody Task task) {
		taskPigstyService.saveTaskPigstyTask(taskpigsty_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Show all TaskPigsty entities
	* 
	*/
	@RequestMapping(value = "/TaskPigsty", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPigsty> listTaskPigstys() {
		return new java.util.ArrayList<TaskPigsty>(taskPigstyService.loadTaskPigstys());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigstyTask(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_task_id) {
		taskPigstyService.deleteTaskPigstyTask(taskpigsty_id, related_task_id);
	}

	/**
	* Delete an existing FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/feedTaskQualities/{feedtaskquality_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigstyFeedTaskQualities(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_feedtaskqualities_id) {
		taskPigstyService.deleteTaskPigstyFeedTaskQualities(taskpigsty_id, related_feedtaskqualities_id);
	}

	/**
	* Create a new PigType entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigType", method = RequestMethod.POST)
	@ResponseBody
	public PigType newTaskPigstyPigType(@PathVariable Integer taskpigsty_id, @RequestBody PigType pigtype) {
		taskPigstyService.saveTaskPigstyPigType(taskpigsty_id, pigtype);
		return pigTypeDAO.findPigTypeByPrimaryKey(pigtype.getId());
	}

	/**
	* Create a new FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/feedTaskQualities", method = RequestMethod.POST)
	@ResponseBody
	public FeedTaskQuality newTaskPigstyFeedTaskQualities(@PathVariable Integer taskpigsty_id, @RequestBody FeedTaskQuality feedtaskquality) {
		taskPigstyService.saveTaskPigstyFeedTaskQualities(taskpigsty_id, feedtaskquality);
		return feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskquality.getId());
	}

	/**
	* View an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigsty/{pigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty loadTaskPigstyPigsty(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_pigsty_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		return pigsty;
	}

	/**
	* Delete an existing PigType entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigType/{pigtype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigstyPigType(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_pigtype_id) {
		taskPigstyService.deleteTaskPigstyPigType(taskpigsty_id, related_pigtype_id);
	}

	/**
	* Save an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigsty", method = RequestMethod.PUT)
	@ResponseBody
	public Pigsty saveTaskPigstyPigsty(@PathVariable Integer taskpigsty_id, @RequestBody Pigsty pigsty) {
		taskPigstyService.saveTaskPigstyPigsty(taskpigsty_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Create a new TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty", method = RequestMethod.POST)
	@ResponseBody
	public TaskPigsty newTaskPigsty(@RequestBody TaskPigsty taskpigsty) {
		taskPigstyService.saveTaskPigsty(taskpigsty);
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty.getId());
	}

	/**
	* Select an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPigsty loadTaskPigsty(@PathVariable Integer taskpigsty_id) {
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id);
	}

	/**
	* View an existing FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/feedTaskQualities/{feedtaskquality_id}", method = RequestMethod.GET)
	@ResponseBody
	public FeedTaskQuality loadTaskPigstyFeedTaskQualities(@PathVariable Integer taskpigsty_id, @PathVariable Integer related_feedtaskqualities_id) {
		FeedTaskQuality feedtaskquality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(related_feedtaskqualities_id, -1, -1);

		return feedtaskquality;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Save an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPigsty saveTaskPigsty(@RequestBody TaskPigsty taskpigsty) {
		taskPigstyService.saveTaskPigsty(taskpigsty);
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty.getId());
	}

	/**
	* Delete an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigsty(@PathVariable Integer taskpigsty_id) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id);
		taskPigstyService.deleteTaskPigsty(taskpigsty);
	}

	/**
	* Create a new Pigsty entity
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/pigsty", method = RequestMethod.POST)
	@ResponseBody
	public Pigsty newTaskPigstyPigsty(@PathVariable Integer taskpigsty_id, @RequestBody Pigsty pigsty) {
		taskPigstyService.saveTaskPigstyPigsty(taskpigsty_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Get Task entity by TaskPigsty
	* 
	*/
	@RequestMapping(value = "/TaskPigsty/{taskpigsty_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getTaskPigstyTask(@PathVariable Integer taskpigsty_id) {
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id).getTask();
	}
}